{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3b717685",
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "\n",
    "import sys\n",
    "import json\n",
    "import time\n",
    "\n",
    "IS_PY3 = sys.version_info.major == 3\n",
    "\n",
    "if IS_PY3:\n",
    "    from urllib.request import urlopen\n",
    "    from urllib.request import Request\n",
    "    from urllib.error import URLError\n",
    "    from urllib.parse import urlencode\n",
    "\n",
    "    timer = time.perf_counter\n",
    "else:\n",
    "    import urllib2\n",
    "    from urllib2 import urlopen\n",
    "    from urllib2 import Request\n",
    "    from urllib2 import URLError\n",
    "    from urllib import urlencode\n",
    "\n",
    "    if sys.platform == \"win32\":\n",
    "        timer = time.clock\n",
    "    else:\n",
    "        # On most other platforms the best timer is time.time()\n",
    "        timer = time.time\n",
    "\n",
    "API_KEY = 'LlfEgXvRWxajUe44Q3eiSGqI'\n",
    "SECRET_KEY = 'gKxHMKamyWP9U5XX7KaknsnMIVXhC3Vi'\n",
    "\n",
    "# 需要识别的文件\n",
    "AUDIO_FILE = './audio/16k.pcm'  # 只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "# 文件格式\n",
    "FORMAT = AUDIO_FILE[-3:];  # 文件后缀只支持 pcm/wav/amr 格式，极速版额外支持m4a 格式\n",
    "\n",
    "CUID = '123456PYTHON';\n",
    "# 采样率\n",
    "RATE = 16000;  # 固定值\n",
    "\n",
    "# 普通版\n",
    "\n",
    "DEV_PID = 1537;  # 1537 表示识别普通话，使用输入法模型。根据文档填写PID，选择语言及识别模型\n",
    "ASR_URL = 'http://vop.baidu.com/server_api'\n",
    "SCOPE = 'audio_voice_assistant_get'  # 有此scope表示有asr能力，没有请在网页里勾选，非常旧的应用可能没有\n",
    "\n",
    "#测试自训练平台需要打开以下信息， 自训练平台模型上线后，您会看见 第二步：“”获取专属模型参数pid:8001，modelid:1234”，按照这个信息获取 dev_pid=8001，lm_id=1234\n",
    "# DEV_PID = 8001 ;   \n",
    "# LM_ID = 1234 ;\n",
    "\n",
    "# 极速版 打开注释的话请填写自己申请的appkey appSecret ，并在网页中开通极速版（开通后可能会收费）\n",
    "\n",
    "#DEV_PID = 80001\n",
    "#ASR_URL = 'http://vop.baidu.com/pro_api'\n",
    "#SCOPE = 'brain_enhanced_asr'  # 有此scope表示有asr能力，没有请在网页里开通极速版\n",
    "\n",
    "# 忽略scope检查，非常旧的应用可能没有\n",
    "# SCOPE = False\n",
    "\n",
    "\n",
    "# 极速版\n",
    "\n",
    "class DemoError(Exception):\n",
    "    pass\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN start \"\"\"\n",
    "\n",
    "TOKEN_URL = 'http://aip.baidubce.com/oauth/2.0/token'\n",
    "\n",
    "\n",
    "def fetch_token(API_KEY,SECRET_KEY):\n",
    "    params = {'grant_type': 'client_credentials',\n",
    "              'client_id': API_KEY,\n",
    "              'client_secret': SECRET_KEY}\n",
    "    post_data = urlencode(params)\n",
    "    if (IS_PY3):\n",
    "        post_data = post_data.encode('utf-8')\n",
    "    req = Request(TOKEN_URL, post_data)\n",
    "    try:\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "    except URLError as err:\n",
    "        print('token http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "    if (IS_PY3):\n",
    "        result_str = result_str.decode()\n",
    "\n",
    "   # print(result_str)\n",
    "    result = json.loads(result_str)\n",
    "   # print(result)\n",
    "    if ('access_token' in result.keys() and 'scope' in result.keys()):\n",
    "        if SCOPE and (not SCOPE in result['scope'].split(' ')):  # SCOPE = False 忽略检查\n",
    "            raise DemoError('scope is not correct')\n",
    "       # print('SUCCESS WITH TOKEN: %s ; EXPIRES IN SECONDS: %s' % (result['access_token'], result['expires_in']))\n",
    "        return result['access_token']\n",
    "    else:\n",
    "        raise DemoError('MAYBE API_KEY or SECRET_KEY not correct: access_token or scope not found in token response')\n",
    "\n",
    "\n",
    "\"\"\"  TOKEN end \"\"\"\n",
    "\n",
    "def asr(token,AUDIO_FILE):\n",
    "\n",
    "    \"\"\"\n",
    "    httpHandler = urllib2.HTTPHandler(debuglevel=1)\n",
    "    opener = urllib2.build_opener(httpHandler)\n",
    "    urllib2.install_opener(opener)\n",
    "    \"\"\"\n",
    "\n",
    "    speech_data = []\n",
    "    with open(AUDIO_FILE, 'rb') as speech_file:\n",
    "        speech_data = speech_file.read()\n",
    "    length = len(speech_data)\n",
    "    if length == 0:\n",
    "        raise DemoError('file %s length read 0 bytes' % AUDIO_FILE)\n",
    "\n",
    "    params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID}\n",
    "    #测试自训练平台需要打开以下信息\n",
    "    #params = {'cuid': CUID, 'token': token, 'dev_pid': DEV_PID, 'lm_id' : LM_ID}\n",
    "    params_query = urlencode(params);\n",
    "\n",
    "    headers = {\n",
    "        'Content-Type': 'audio/' + FORMAT + '; rate=' + str(RATE),\n",
    "        'Content-Length': length\n",
    "    }\n",
    "\n",
    "    url = ASR_URL + \"?\" + params_query\n",
    "#     print(\"url is\", url);\n",
    "#     print(\"header is\", headers)\n",
    "    # print post_data\n",
    "    req = Request(ASR_URL + \"?\" + params_query, speech_data, headers)\n",
    "    try:\n",
    "        begin = timer()\n",
    "        f = urlopen(req)\n",
    "        result_str = f.read()\n",
    "        print(\"Request time cost %f\" % (timer() - begin))\n",
    "    except  URLError as err:\n",
    "        print('asr http response http code : ' + str(err.code))\n",
    "        result_str = err.read()\n",
    "\n",
    "    if (IS_PY3):\n",
    "        result_str = str(result_str, 'utf-8')\n",
    "#     print(result_str)\n",
    "    with open(\"result.txt\", \"w\") as of:\n",
    "        of.write(result_str)\n",
    "    return result_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "039298f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.07e9ca30d349c0b208331865475a4f49.2592000.1655382377.282335-26256000'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kerwin_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "kerwin_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7e3ffa2a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.347432\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7098680617741631068\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"北京科技馆。\"],\"sn\":\"155163475371652790377\"}\\n'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr(kerwin_token,'audio/16k.wav')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfd899af",
   "metadata": {},
   "source": [
    "## 语音识别自动回复文本机器人\n",
    "\n",
    "1. 准备录制音频\n",
    "2. 调用语音识别，讲音频转成文本\n",
    "3. 文本自动回复"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac01cac7",
   "metadata": {},
   "source": [
    "### 准备音频文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b88570d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting SpeechRecognition\n",
      "  Downloading SpeechRecognition-3.8.1-py2.py3-none-any.whl (32.8 MB)\n",
      "Installing collected packages: SpeechRecognition\n",
      "Successfully installed SpeechRecognition-3.8.1\n"
     ]
    }
   ],
   "source": [
    "!pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8d69668a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pipwin in d:\\anaconda\\lib\\site-packages (0.5.2)\n",
      "Requirement already satisfied: beautifulsoup4>=4.9.0 in d:\\anaconda\\lib\\site-packages (from pipwin) (4.9.3)\n",
      "Requirement already satisfied: pySmartDL>=1.3.1 in d:\\anaconda\\lib\\site-packages (from pipwin) (1.3.4)\n",
      "Requirement already satisfied: requests in d:\\anaconda\\lib\\site-packages (from pipwin) (2.25.1)\n",
      "Requirement already satisfied: six in d:\\anaconda\\lib\\site-packages (from pipwin) (1.15.0)\n",
      "Requirement already satisfied: js2py in d:\\anaconda\\lib\\site-packages (from pipwin) (0.71)\n",
      "Requirement already satisfied: packaging in d:\\anaconda\\lib\\site-packages (from pipwin) (20.9)\n",
      "Requirement already satisfied: docopt in d:\\anaconda\\lib\\site-packages (from pipwin) (0.6.2)\n",
      "Requirement already satisfied: pyprind in d:\\anaconda\\lib\\site-packages (from pipwin) (2.11.3)\n",
      "Requirement already satisfied: soupsieve>1.2 in d:\\anaconda\\lib\\site-packages (from beautifulsoup4>=4.9.0->pipwin) (2.2.1)\n",
      "Requirement already satisfied: pyjsparser>=2.5.1 in d:\\anaconda\\lib\\site-packages (from js2py->pipwin) (2.7.1)\n",
      "Requirement already satisfied: tzlocal>=1.2 in d:\\anaconda\\lib\\site-packages (from js2py->pipwin) (4.2)\n",
      "Requirement already satisfied: backports.zoneinfo in d:\\anaconda\\lib\\site-packages (from tzlocal>=1.2->js2py->pipwin) (0.2.1)\n",
      "Requirement already satisfied: pytz-deprecation-shim in d:\\anaconda\\lib\\site-packages (from tzlocal>=1.2->js2py->pipwin) (0.1.0.post0)\n",
      "Requirement already satisfied: tzdata in d:\\anaconda\\lib\\site-packages (from tzlocal>=1.2->js2py->pipwin) (2022.1)\n",
      "Requirement already satisfied: pyparsing>=2.0.2 in d:\\anaconda\\lib\\site-packages (from packaging->pipwin) (2.4.7)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in d:\\anaconda\\lib\\site-packages (from requests->pipwin) (2020.12.5)\n",
      "Requirement already satisfied: idna<3,>=2.5 in d:\\anaconda\\lib\\site-packages (from requests->pipwin) (2.10)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\\anaconda\\lib\\site-packages (from requests->pipwin) (1.26.4)\n",
      "Requirement already satisfied: chardet<5,>=3.0.2 in d:\\anaconda\\lib\\site-packages (from requests->pipwin) (4.0.0)\n",
      "Package `pyaudio` found in cache\n",
      "Downloading package . . .\n",
      "https://download.lfd.uci.edu/pythonlibs/x6hvwk7i/PyAudio-0.2.11-cp38-cp38-win_amd64.whl\n",
      "PyAudio-0.2.11-cp38-cp38-win_amd64.whl\n",
      "[*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 8 kB / 111 kB @ 5 kB/s [#-----------------] [7.2%, 0s left]   [*] 8 kB / 111 kB @ 5 kB/s [#-----------------] [7.2%, 0s left]   [*] 8 kB / 111 kB @ 4 kB/s [#-----------------] [7.2%, 0s left]   [*] 40 kB / 111 kB @ 21 kB/s [######------------] [36.0%, 0s left]  [*] 40 kB / 111 kB @ 20 kB/s [######------------] [36.0%, 0s left]  [*] 88 kB / 111 kB @ 42 kB/s [##############----] [79.3%, 0s left]  [*] 88 kB / 111 kB @ 40 kB/s [##############----] [79.3%, 0s left]  [*] 88 kB / 111 kB @ 38 kB/s [##############----] [79.3%, 0s left]  Processing c:\\users\\酸奶\\pipwin\\pyaudio-0.2.11-cp38-cp38-win_amd64.whl\n",
      "Installing collected packages: PyAudio\n",
      "Successfully installed PyAudio-0.2.11\n",
      "[*] 111 kB / 111 kB @ 38 kB/s [##################] [100%, 0s left]    \n"
     ]
    }
   ],
   "source": [
    "# !pip install PyAudio\n",
    "\n",
    "!pip install pipwin\n",
    "!pipwin install PyAudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a33f158",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "\n",
    "r = speech_recognition.Recognizer()\n",
    "\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a2fcc4f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你好\":\"你好，有事吗？\",\n",
    "    \"大小姐\":\"说吧，什么事？\",\n",
    "    \"你在干嘛啊\":\"关你屁事啊...\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ae38dd3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好，有事吗？'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa.get('你好')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e0d7bcb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.480031\n"
     ]
    }
   ],
   "source": [
    "# 1. 录制音频文件\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))\n",
    "    \n",
    "# 2. 调用百度asr\n",
    "kerwin_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "\n",
    "asr_result = eval(asr(kerwin_token,\"1.wav\"))['result'][0][:-1]\n",
    "asr_result\n",
    "\n",
    "# 3. 文本自动回复\n",
    "qa.get(asr_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "300e3f39",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
